package cn.wolfcode.memeber.member.service.impl;

import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.domain.UserInfoScore;
import cn.wolfcode.luowowo.member.service.IUserInfoScoreService;
import cn.wolfcode.memeber.member.service.mapper.UserInfoScoreMapper;
import cn.wolfcoe.luowowo.common.exception.LogicException;
import cn.wolfcoe.luowowo.common.util.AjaxResult;
import com.alibaba.dubbo.config.annotation.Service;
import javafx.fxml.LoadException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
@Service
public class UserInfoScoreServiceImpl implements IUserInfoScoreService {
    @Autowired
    private UserInfoScoreMapper userInfoScoreMapper;

    @Override
    public UserInfoScore get(Long id) {
        return userInfoScoreMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(UserInfoScore userInfoScore) {
        if (userInfoScore.getId() == null) {
            userInfoScoreMapper.insert(userInfoScore);
        }else {
            userInfoScoreMapper.updateByPrimaryKey(userInfoScore);
        }
    }

    @Override
    public List<UserInfoScore> list() {
        return userInfoScoreMapper.selectAll();
    }

    @Override//消费用户积分
    public AjaxResult expenditure(UserInfo userInfo, int score) {
        //通过用户id获取用户积分对象
        UserInfoScore userInfoScore = userInfoScoreMapper.selectByPrimaryKey(userInfo.getId());
        if (userInfoScore.getScore()<score) {
            return new AjaxResult(false,"很抱歉,您的积分不足");
        }
        //用户积分对象扣除商品积分
        userInfoScore.setScore(userInfoScore.getScore() - score);
        //更新数据库用户积分
        saveOrUpdate(userInfoScore);
        return AjaxResult.SUCCESS;
    }

    @Override//增加了用户积分
    public void addscore(UserInfo userInfo, Integer score) {
        //通过用户id获取用户积分对象
        UserInfoScore userInfoScore = userInfoScoreMapper.selectByPrimaryKey(userInfo.getId());
        //增加用户积分对象的积分
        userInfoScore.setScore(userInfoScore.getScore() + score);
        //更新数据库用户积分
        saveOrUpdate(userInfoScore);
    }
}
